/**
 * @param {Array} position = [114,30]
 * @param {Array} attr = [
 * {name:"name",type:"string",value:"中国"},
 * {name:"id",type:"number",value:1001}
 * ]
 * @param {Object} service ={name:"igserver服务的名称",layerId:"添加要素所在的图层"}
 * @param {Object} docLayer 地图文档的图层
 * */
const POINST_STYLE = {
    Angle: 0,
    Color: 11, //子图颜色
    Space: 0,
    SymHeight: 12,//高度
    SymWidth: 12, //宽度
    SymID: 31,//子图号
}
class Point {
    static add({
        position,
        attr,
        service,
        docLayer
    }) {
        /* 1、创建mapgis要素 */
        /* 1-1、构建几何信息 geometry */
        var gpoint = new Zondy.Object.GPoint(position[0], position[1]);
        //设置当前点要素的几何信息
        var fGeom = new Zondy.Object.FeatureGeometry({ PntGeom: [gpoint] });
        /* 1-2、构建样式 */
        //描述点要素的符号参数信息
        var pointInfo = new Zondy.Object.CPointInfo(POINST_STYLE);
        //设置当前点要素的图形参数信息
        var webGraphicInfo = new Zondy.Object.WebGraphicsInfo({
            InfoType: 1,
            PntInfo: pointInfo
        });
        /* 1-3、属性 */
        //设置添加点要素的属性信息        
        var attValue = attr.map(item => item.value);
        //创建一个要素
        var feature = new Zondy.Object.Feature({
            fGeom: fGeom,
            GraphicInfo: webGraphicInfo,
            AttValue: attValue
        });
        //设置要素为点要素  1 Point 2LineString 3Polygon
        feature.setFType(1);
        /* 2、将要素添加要素集 */
        var featureSet = new Zondy.Object.FeatureSet();
        //设置属性结构  测试看是否为必选
        var cAttStruct = new Zondy.Object.CAttStruct({
            FldName: attr.map(item => item.name),
            FldNumber: attr.length,
            FldType: attr.map(item => item.type)
        });
        featureSet.AttStruct = cAttStruct;
        //添加要素到要素数据集
        featureSet.addFeature(feature);

        /* 3、调用服务添加要素【http】 */
        /* 第一个参数  ig servre服务的名称 */
        /* 第二个参数 是添加要素所在的图层 Point-0 */
        var editService = new Zondy.Service.EditDocFeature(service.name, service.layerId, {
        });
        //执行添加点要素功能
        editService.add(featureSet, this.onSuccess(docLayer, '添加'));
    }
    static onSuccess(docLayer, optionType) {
        return function (data) {
            if (data.succeed) {
                alert(`${optionType}点要素成功！`);
                docLayer.refresh();
            } else {
                alert(`${optionType}点要素失败！`);
            }
        }
    }
    static del () {

    }
    static query ({
        position, 
        service,
        callback}) {
        const pointObj = new Zondy.Object.Point2D(position[0], position[1]);
            //设置查询点的搜索半径
            pointObj.nearDis = 2;
    var queryStruct = new Zondy.Service.QueryFeatureStruct();
            //是否包含几何图形信息
            queryStruct.IncludeGeometry = true;
            //是否包含属性信息
            queryStruct.IncludeAttribute = true;
            //是否包含图形显示参数
            queryStruct.IncludeWebGraphic = false;
            //指定查询规则
            var rule = new Zondy.Service.QueryFeatureRule({
                //是否将要素的可见性计算在内
                EnableDisplayCondition: false,
                //是否完全包含
                MustInside: false,
                //是否仅比较要素的外包矩形
                CompareRectOnly: false,
                //是否相交
                Intersect: true
            });
            //实例化查询参数对象
            var queryParam = new Zondy.Service.QueryParameter({
                r: Math.random(),
                geometry: pointObj,
                resultFormat: "json",
                struct: queryStruct,
                rule: rule
            })
             //设置查询分页号
             queryParam.pageIndex = 0;
             //设置查询要素数目
             queryParam.recordNumber = 100;
             //实例化地图文档查询服务对象
             var queryService = new Zondy.Service.QueryDocFeature(queryParam, service.name, service.layerId, {});
            //执行查询操作，querySuccess为查询回调函数
            queryService.query(this.querySuccess(callback));
            
    }
    static querySuccess (data) {
        return function () {
            console.log('123', data);
            //初始化Zondy.Format.PolygonJSON类
            var format = new Zondy.Format.PolygonJSON();
            //将MapGIS要素JSON反序列化为ol.Feature类型数组
            var features = format.read(data);
            console.log('123', data, features);
            callback(features)
        }
    }

        static queryByRect ({geometry, service, callback}) {
            //初始化查询结构对象，设置查询结构包含几何信息
                var queryStruct = new Zondy.Service.QueryFeatureStruct();
                //是否包含几何图形信息
                queryStruct.IncludeGeometry = true;
                //是否包含属性信息
                queryStruct.IncludeAttribute = true;
                //是否包含图形显示参数
                queryStruct.IncludeWebGraphic = false;
                //创建一个用于查询的区
                var geomObj = new Zondy.Object.Polygon();
                geomObj.setByOL(geometry);
                //指定查询规则
                var rule = new Zondy.Service.QueryFeatureRule({
                    //是否将要素的可见性计算在内
                    EnableDisplayCondition: false,
                    //是否完全包含
                    MustInside: false,
                    //是否仅比较要素的外包矩形
                    CompareRectOnly: false,
                    //是否相交
                    Intersect: true
                });
                //实例化查询参数对象
                var queryParam = new Zondy.Service.QueryParameter({
                    geometry: geomObj,
                    resultFormat: "json",
                    struct: queryStruct,
                    rule: rule
                });
                //设置查询分页号
                queryParam.pageIndex = 0;
                //设置查询要素数目
                queryParam.recordNumber = 20;
                //实例化地图文档查询服务对象
                var queryService = new Zondy.Service.QueryDocFeature(queryParam, service.name, service.layerId, {});
                //执行查询操作，querySuccess为查询回调函数
                queryService.query(this.querySuccess(callback));
        }
        static queryByGeom({
            geometry,
            service,
            callback
        }) {
            //初始化查询结构对象，设置查询结构包含几何信息
            var queryStruct = new Zondy.Service.QueryFeatureStruct();
            //是否包含几何图形信息
            queryStruct.IncludeGeometry = true;
            //是否包含属性信息
            queryStruct.IncludeAttribute = true;
            //是否包含图形显示参数
            queryStruct.IncludeWebGraphic = false;
    
            //创建一个用于查询的区
            var geomObj = new Zondy.Object.Polygon();
            geomObj.setByOL(geometry);
    
    
            //指定查询规则
            var rule = new Zondy.Service.QueryFeatureRule({
                //是否将要素的可见性计算在内
                EnableDisplayCondition: false,
                //是否完全包含
                MustInside: false,
                //是否仅比较要素的外包矩形
                CompareRectOnly: false,
                //是否相交
                Intersect: true
            });
            //实例化查询参数对象
            var queryParam = new Zondy.Service.QueryParameter({
                geometry: geomObj,
                resultFormat: "json",
                struct: queryStruct,
                rule: rule
            });
            //设置查询分页号
            queryParam.pageIndex = 0;
            //设置查询要素数目
            queryParam.recordNumber = 1000;
            //实例化地图文档查询服务对象
            var queryService = new Zondy.Service.QueryDocFeature(queryParam, service.name, service.layerId, {
    
            });
            //执行查询操作，querySuccess为查询回调函数
            queryService.query(callback);
        }
    }